package com.qf.common.http;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.Serializable;
import java.util.List;

@JsonSerialize
@JsonInclude(value = JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable{

    private static final long serialVersionUID =1L;
    private Integer code;
    private String error;
    private Boolean ok;
    private T data;
    private Long count;


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public Boolean getOk() {
        return ok;
    }

    public void setOk(Boolean ok) {
        this.ok = ok;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    public Result(Integer code, String error, Boolean ok, T data) {
        this.code = code;
        this.error = error;
        this.ok = ok;
        this.data = data;
    }

    public static <T> Result<T> create(ResultCode resultCode,T data){
        return new Result<T>(resultCode.getCode(),resultCode.getDescription(),resultCode.getOk(),data);
    }

    public static <T> Result<T> create(ResultCode resultCode){
        return new Result<T>(resultCode.getCode(),resultCode.getDescription(),resultCode.getOk(),null);
    }

    /**
     * 创建成功信息
     *
     * @param data 返回给客户端的信息
     * @return
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCode.SUCCESS.getCode(), null, ResultCode.SUCCESS.getOk(),data);
    }

    /**
     * 创建成功信息
     *
     * @param page 返回给客户端的信息
     * @return
     */
    public static <T> Result<List<T>> success(IPage<T> page) {
        Result<List<T>> result = new Result<List<T>>(ResultCode.SUCCESS.getCode(), null,
                ResultCode.SUCCESS.getOk(), page.getRecords());
        result.setCount(page.getTotal());
        return result;
    }

    /**
     * 创建成功信息
     *
     * @return
     */
    public static <T> Result<T> success() {
        return new Result<T>(ResultCode.SUCCESS.getCode(), null, ResultCode.SUCCESS.getOk(),null);
    }

    /**
     * 创建失败信息
     *
     * @param data 返回给客户端的信息
     * @return
     */
    public static <T> Result<T> error(T data) {
        return new Result<T>(ResultCode.ERROR.getCode(), null, ResultCode.ERROR.getOk(), data);
    }

    /**
     * 创建失败信息
     *
     * @param error 返回给客户端的信息
     * @return
     */
    public static <T> Result<T> error(String error) {
        return new Result<T>(ResultCode.ERROR.getCode(), error, ResultCode.ERROR.getOk(), null);
    }

    /**
     * 创建失败信息
     *
     * @return
     */
    public static <T> Result<T> error() {
        return new Result<T>(ResultCode.ERROR.getCode(), null,  ResultCode.ERROR.getOk(),null);
    }


}
